﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Sobiens.Web.Controls.Samples
{
    public partial class _Default : System.Web.UI.Page
    {
        DataSet dataset;
        protected void Page_Load(object sender, EventArgs e)
        {
            // Put user code to initialize the page here
            dataset = new DataSet();

            this.LoadCountries();
            this.LoadUsers();

            DataRelation custOrderRel = dataset.Relations.Add("UserCountry",
                dataset.Tables["Countries"].Columns["ID"],
                dataset.Tables["Users"].Columns["Country"], false
                );
            SDXGrid1.DataSource = dataset;
            SDXGrid1.DataMember = "Users";
            SDXGrid1.DataBind();
        }
        private void LoadUsers()
        {
            DataTable users = dataset.Tables.Add("Users");
            users.Columns.Add("ID", typeof(int));
            users.Columns.Add("Name");
            users.Columns.Add("Sex");
            users.Columns.Add("Age");
            users.Columns.Add("Married", typeof(bool));
            users.Columns.Add("BirthDate");
            users.Columns.Add("Country");
            users.Columns.Add("Note");

            DataRow user1 = users.NewRow();
            user1["ID"] = 1;
            user1["Name"] = "Baran Ozgul";
            user1["Sex"] = "M";
            user1["Age"] = "27";
            user1["Married"] = true;
            user1["BirthDate"] = "22/02/1978";
            user1["Country"] = 5;
            users.Rows.Add(user1);

            DataRow user2 = users.NewRow();
            user2["ID"] = 2;
            user2["Name"] = "Serkant Samurkas";
            user2["Sex"] = "M";
            user2["Age"] = "28";
            user2["Married"] = false;
            user2["BirthDate"] = "24/04/1979";
            user2["Country"] = 5;
            users.Rows.Add(user2);

            DataRow user4 = users.NewRow();
            user4["ID"] = 4;
            user4["Name"] = "Ozgur Civi";
            user4["Sex"] = "M";
            user4["Age"] = "28";
            user4["Married"] = true;
            user4["BirthDate"] = "01/02/1977";
            user4["Country"] = 4;
            users.Rows.Add(user4);

            DataRow user5 = users.NewRow();
            user5["ID"] = 5;
            user5["Name"] = "Tony Ratray";
            user5["Sex"] = "M";
            user5["Age"] = "31";
            user5["Married"] = true;
            user5["BirthDate"] = "12/11/1974";
            user5["Country"] = 5;
            users.Rows.Add(user5);

            DataRow user6 = users.NewRow();
            user6["ID"] = 6;
            user6["Name"] = "Nara Meliki";
            user6["Sex"] = "F";
            user6["Age"] = "24";
            user6["Married"] = false;
            user6["BirthDate"] = "07/02/1981";
            user6["Country"] = 5;
            users.Rows.Add(user6);

            DataRow user7 = users.NewRow();
            user7["ID"] = 7;
            user7["Name"] = "Narod Karol";
            user7["Sex"] = "F";
            user7["Age"] = "23";
            user7["Married"] = false;
            user7["BirthDate"] = "22/02/1982";
            user7["Country"] = 6;
            users.Rows.Add(user7);

            DataRow user8 = users.NewRow();
            user8["ID"] = 8;
            user8["Name"] = "Janet Cooper";
            user8["Sex"] = "F";
            user8["Age"] = "27";
            user8["Married"] = true;
            user8["BirthDate"] = "22/02/1978";
            user8["Country"] = 1;
            users.Rows.Add(user8);

            DataRow user9 = users.NewRow();
            user9["ID"] = 9;
            user9["Name"] = "Cesminaz Karabulut";
            user9["Sex"] = "F";
            user9["Age"] = "24";
            user9["Married"] = false;
            user9["BirthDate"] = "22/02/1981";
            user9["Country"] = 4;
            users.Rows.Add(user9);

            DataRow user10 = users.NewRow();
            user10["ID"] = 10;
            user10["Name"] = "Ziver Alen Malhasoglu";
            user10["Sex"] = "M";
            user10["Age"] = "25";
            user10["Married"] = false;
            user10["BirthDate"] = "22/02/1980";
            user10["Country"] = 3;
            users.Rows.Add(user10);

        }
        private void LoadCountries()
        {
            DataTable countries = dataset.Tables.Add("Countries");
            countries.Columns.Add("ID");
            countries.Columns.Add("Name");

            DataRow country1 = countries.NewRow();
            country1["ID"] = 1;
            country1["Name"] = "France";
            countries.Rows.Add(country1);

            DataRow country2 = countries.NewRow();
            country2["ID"] = 2;
            country2["Name"] = "Germany";
            countries.Rows.Add(country2);

            DataRow country3 = countries.NewRow();
            country3["ID"] = 3;
            country3["Name"] = "Italy";
            countries.Rows.Add(country3);

            DataRow country4 = countries.NewRow();
            country4["ID"] = 4;
            country4["Name"] = "Turkey";
            countries.Rows.Add(country4);

            DataRow country5 = countries.NewRow();
            country5["ID"] = 5;
            country5["Name"] = "UK";
            countries.Rows.Add(country5);

            DataRow country6 = countries.NewRow();
            country6["ID"] = 6;
            country6["Name"] = "USA";
            countries.Rows.Add(country6);

            DataRow country7 = countries.NewRow();
            country7["ID"] = 7;
            country7["Name"] = "Ireland";
            countries.Rows.Add(country7);

        }
        public void SDXGrid1_OnUpdate(object sender, Sobiens.Web.Controls.SDXDataGrid.OnUpdateEventArgs e)
        {
            DataTable table = e.Dataset.Tables["Users"];
            SDXGrid1.AcceptChanges();
            if (table.GetChanges(System.Data.DataRowState.Added) != null)
                System.Web.HttpContext.Current.Response.Write("Added Count:" + table.GetChanges(System.Data.DataRowState.Added).Rows.Count.ToString() + Environment.NewLine);
            if (table.GetChanges(System.Data.DataRowState.Modified) != null)
                System.Web.HttpContext.Current.Response.Write("Modified Count:" + table.GetChanges(System.Data.DataRowState.Modified).Rows.Count.ToString() + Environment.NewLine);
            if (table.GetChanges(System.Data.DataRowState.Deleted) != null)
                System.Web.HttpContext.Current.Response.Write("Deleted Count:" + table.GetChanges(System.Data.DataRowState.Deleted).Rows.Count.ToString() + Environment.NewLine);
        }

    }
}
